package com.dream.keigezhushou.Activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.bean.LingshiInfo;
import com.dream.keigezhushou.Activity.bean.NearKtvDetilsInfo;
import com.dream.keigezhushou.Activity.bean.TeamInfo;
import com.dream.keigezhushou.Activity.bean.TimeInfo;
import com.dream.keigezhushou.Activity.wigdt.MyListView;
import com.dream.keigezhushou.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class KtvDetailsActivity extends AppCompatActivity {

    @BindView(R.id.shoucang)
    ImageView mShoucang;//收藏
    @BindView(R.id.share)
    ImageView mShare;//分享
    @BindView(R.id.ivicon)
    ImageView mIvicon;//ktv图片
    @BindView(R.id.ktvtitle)
    TextView mKtvtitle;//ktv名称
    @BindView(R.id.ratingBar_1)
    RatingBar mRatingBar1;//星评分
    @BindView(R.id.pf)
    TextView mPf;//数字分
    @BindView(R.id.meanprice)
    TextView mMeanprice;//均价
    @BindView(R.id.location_iv)
    ImageView mLocationIv;//定位
    @BindView(R.id.location)
    TextView mLocation;//地址
    @BindView(R.id.phone)
    ImageView mPhone;//电话
    @BindView(R.id.personnumber)
    TextView mPersonnumber;//人数已预订
    @BindView(R.id.tv_1)
    TextView mTv1;//
    @BindView(R.id.data_1)
    TextView mData1;
    @BindView(R.id.view_1)
    View mView1;
    @BindView(R.id.time1)
    RelativeLayout mTime1;
    @BindView(R.id.data_2)
    TextView mData2;
    @BindView(R.id.view_2)
    View mView2;
    @BindView(R.id.tv_2)
    TextView mTv2;
    @BindView(R.id.time2)
    RelativeLayout mTime2;
    @BindView(R.id.tv_3)
    TextView mTv3;
    @BindView(R.id.data_3)
    TextView mData3;
    @BindView(R.id.view_3)
    View mView3;
    @BindView(R.id.time3)
    RelativeLayout mTime3;
    @BindView(R.id.tv_4)
    TextView mTv4;
    @BindView(R.id.data_4)
    TextView mData4;
    @BindView(R.id.view_4)
    View mView4;
    @BindView(R.id.time4)
    RelativeLayout mTime4;
    @BindView(R.id.tv_5)
    TextView mTv5;
    @BindView(R.id.data_5)
    TextView mData5;
    @BindView(R.id.view_5)
    View mView5;
    @BindView(R.id.time5)
    RelativeLayout mTime5;
    @BindView(R.id.tv_6)
    TextView mTv6;
    @BindView(R.id.data_6)
    TextView mData6;
    @BindView(R.id.view_6)
    View mView6;
    @BindView(R.id.time6)
    RelativeLayout mTime6;
    @BindView(R.id.tv_7)
    TextView mTv7;
    @BindView(R.id.data_7)
    TextView mData7;
    @BindView(R.id.view_7)
    View mView7;
    @BindView(R.id.time7)
    RelativeLayout mTime7;
    @BindView(R.id.type_1)
    TextView mType1;
    @BindView(R.id.type_2)
    TextView mType2;
    @BindView(R.id.type_3)
    TextView mType3;
    @BindView(R.id.timelist)
    MyListView mTimelist;//时间段集合
    @BindView(R.id.team)
    TextView mTeam;//团购
    @BindView(R.id.scattered)
    TextView mScattered;//零食
    @BindView(R.id.tv_js)
    TextView mTvJs;//酒水
    @BindView(R.id.tv_ls)
    TextView mTvLs;//零食
    @BindView(R.id.tv_sg)
    TextView mTvSg;//水果
    @BindView(R.id.tv_qt)
    TextView mTvQt;//其他
    @BindView(R.id.teamlist)
    MyListView mTeamlist;//团购集合
    @BindView(R.id.ktvlist)
    MyListView mKtvlist;//附近Ktv集合

    @BindView(R.id.back)
    ImageView mBack;//返回
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.rj)
    TextView mRj;
    @BindView(R.id.yj)
    TextView mYj;
    @BindView(R.id.actionbar)
    RelativeLayout mActionbar;
    @BindView(R.id.look)
    TextView mLook;
    @BindView(R.id.ll)
    LinearLayout mLl;
    @BindView(R.id.activity_ktv_details)
    LinearLayout mActivityKtvDetails;
    @BindView(R.id.timeparent)
    LinearLayout mTimeparent;

    private ArrayList<TimeInfo> timeList = new ArrayList<>();
    private ArrayList<TeamInfo> teamList = new ArrayList<>();
    private ArrayList<LingshiInfo> lingshiList = new ArrayList<>();
    private ArrayList<NearKtvDetilsInfo> nearktvList = new ArrayList<>();


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //设置状态栏颜色
        window.setStatusBarColor(getResources().getColor(R.color.seting));

        ViewGroup mContentView = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 预留出系统 View 的空间.
            ViewCompat.setFitsSystemWindows(mChildView, true);
        }
        setContentView(R.layout.activity_ktv_details);
        ButterKnife.bind(this);
        initdata();
//        setListViewHeightBasedOnChildren(mTimelist);
//        setListViewHeightBasedOnChildren(mTeamlist);
//        setListViewHeightBasedOnChildren(mKtvlist);



        //加载动画
        //获取数据url
        //假数据

        mTimelist.setAdapter(new TimeAdapter());
        mTeamlist.setAdapter(new TeamAdapter());
        mKtvlist.setAdapter(new MyAdapter());

        mTeamlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //传数据


                //跳页面
                Intent intent = new Intent(KtvDetailsActivity.this, TeamBuyDetailsActivity.class);

                startActivity(intent);


            }
        });


    }
//    private void setListViewHeightBasedOnChildren(ListView listView) {
//        ListAdapter listAdapter = listView.getAdapter();
//        if (listAdapter == null) {
//            // pre-condition
//            return;
//        }
//
//        int totalHeight = 0;
//        for (int i = 0; i < listAdapter.getCount(); i++) {
//            View listItem = listAdapter.getView(i, null, listView);
//            listItem.measure(0, 0);
//            totalHeight += listItem.getMeasuredHeight();
//        }
//
//        ViewGroup.LayoutParams params = listView.getLayoutParams();
//        params.height = totalHeight
//                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
//        listView.setLayoutParams(params);
//    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.from_right_in, R.anim.to_left_out);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.from_left_in, R.anim.to_right_out);
    }

    private void initdata() {
        mIvicon.setImageDrawable(getResources().getDrawable(R.mipmap.ivicon));
        mKtvtitle.setText("乐唱好声音KTV");
        mRatingBar1.setRating(4.0f);
        mPf.setText("4" + "分");
        mMeanprice.setText("50");
        mLocation.setText("未央区凤城二路东段（近未央路）");
        mPersonnumber.setText("2937" + "人订过");
        mTime1.setSelected(true);
        mType1.setSelected(true);
        mTeam.setSelected(true);
        mView1.setVisibility(View.VISIBLE);
        for (int i = 0; i < 5; i++) {
            TimeInfo info = new TimeInfo();
            info.setDetails("欢唱6小时+套餐");
            info.setPrice("68");
            info.setTimes("13:00-19:00");
            timeList.add(info);
        }

        for (int g = 0; g < 6; g++) {
            NearKtvDetilsInfo info = new NearKtvDetilsInfo();
            info.setCombo("［未央路］周一至周日时段3选1欢唱＋小吃饮品套餐");
            info.setSaleprice("794");
            info.setPrice("188");
            info.setDistance("<500m");
            info.setName("乐唱好声音KTV");
            info.setIv(getResources().getDrawable(R.mipmap.iconiv));

            nearktvList.add(info);
        }
        for (int j = 0; j < 10; j++) {
            TeamInfo info = new TeamInfo();
            info.setPrice("188");
            info.setCombo("周一至周日时段3选1欢唱+小吃饮品套餐");
            info.setSaleprice("794");
            info.setYishou("2025");
            info.setIv(getResources().getDrawable(R.mipmap.team_icon));
            teamList.add(info);

        }
        for (int k = 0; k < 6; k++) {
            LingshiInfo info = new LingshiInfo();
            info.setIv(getResources().getDrawable(R.mipmap.ivls));
            info.setPrice("66.66");
            info.setTvname("原装青岛纯生250ml＊1瓶");
            lingshiList.add(info);
        }



    }


    @OnClick({R.id.look, R.id.back, R.id.shoucang, R.id.share, R.id.location_iv, R.id.phone, R.id.time1, R.id.time2, R.id.time3, R.id.time4, R.id.time5, R.id.time6, R.id.time7, R.id.type_1, R.id.type_2, R.id.type_3, R.id.team, R.id.scattered, R.id.tv_js, R.id.tv_ls, R.id.tv_sg, R.id.tv_qt, R.id.actionbar})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.shoucang:
                //添加到集合
                UiUtils.toast("收藏成功");
                break;
            case R.id.share:
                //集成sdk

                break;
            case R.id.location_iv:
                //集成百度地图
                // startActivity();
                break;
            case R.id.phone:
                //获取电话号码并拨打

                break;
            case R.id.time1:
                setSelect(0);



                break;
            case R.id.time2:
                setSelect(1);

                break;
            case R.id.time3:
                setSelect(2);
                break;
            case R.id.time4:
                setSelect(3);
                break;
            case R.id.time5:
                setSelect(4);
                break;
            case R.id.time6:
                setSelect(5);
                break;
            case R.id.time7:
                setSelect(6);
                break;
            case R.id.type_1:
                setSelectType(0);

                mTimelist.setAdapter(new TimeAdapter());

                break;
            case R.id.type_2:
                setSelectType(1);
                break;
            case R.id.type_3:
                setSelectType(2);
                break;
            case R.id.team:
                setSelectNum(3);
                mTeamlist.setAdapter(new TeamAdapter());
                break;
            case R.id.scattered:
                setSelectNum(4);
                mLl.setVisibility(View.VISIBLE);
                mTvJs.setSelected(true);

                mTeamlist.setAdapter(new LsAdapter());

                break;
            case R.id.tv_js:
                setSelectTypeNum(5);
                break;
            case R.id.tv_ls:
                setSelectTypeNum(6);
                break;
            case R.id.tv_sg:
                setSelectTypeNum(7);
                break;
            case R.id.tv_qt:
                setSelectTypeNum(8);
                break;
            case R.id.actionbar:
                break;
            case R.id.look:
                break;
        }


    }

    public void setSelect(int number) {


        mView1.setVisibility(number == 0 ? View.VISIBLE : View.GONE);
        mData1.setSelected(number == 0  ? true : false);
        mTime1.setSelected(number == 0  ? true : false);
        mType1.setSelected(number==0?true:false);
        mView2.setVisibility(number == 1 ? View.VISIBLE : View.GONE);
        mData2.setSelected(number == 1 ? true : false);
        mTime2.setSelected(number == 1 ? true : false);
        mView3.setVisibility(number == 2 ? View.VISIBLE : View.GONE);
        mData3.setSelected(number == 2  ? true : false);
        mTime3.setSelected(number == 2  ? true : false);
        mView4.setVisibility(number == 3 ? View.VISIBLE : View.GONE);
        mData4.setSelected(number == 3  ? true : false);
        mTime4.setSelected(number == 3? true : false);
        mView5.setVisibility(number == 4 ? View.VISIBLE : View.GONE);
        mData5.setSelected(number == 4? true : false);
        mTime5.setSelected(number == 4 ? true : false);
        mView6.setVisibility(number == 5 ? View.VISIBLE : View.GONE);
        mData6.setSelected(number == 5? true : false);
        mTime6.setSelected(number == 5 ? true : false);
        mView7.setVisibility(number == 6 ? View.VISIBLE : View.GONE);
        mData7.setSelected(number == 6 ? true : false);
        mTime7.setSelected(number == 6 ? true : false);

}
    public void setSelectType(int number) {
        mType1.setSelected(number==0?true:false);
        mType2.setSelected(number==1?true:false);
        mType3.setSelected(number==2?true:false);

    }
    public void setSelectNum(int number){
        mTeam.setSelected(number==3?true:false);
        mScattered.setSelected(number==4?true:false);
    }
    public void setSelectTypeNum(int number) {
        mTvJs.setSelected(number==5?true:false);
        mTvLs.setSelected(number==6?true:false);
        mTvSg.setSelected(number==7?true:false);
        mTvQt.setSelected(number==8?true:false);
    }


class TimeAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return timeList.size();
    }

    @Override
    public TimeInfo getItem(int position) {
        return timeList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TimeViewHolder timeholder = null;
        if (convertView == null) {
            convertView = UiUtils.inflateView(R.layout.item_time);
            timeholder = new TimeViewHolder();
            timeholder.times = (TextView) convertView.findViewById(R.id.time_1);
            timeholder.details = (TextView) convertView.findViewById(R.id.des_1);
            timeholder.price = (TextView) convertView.findViewById(R.id.price_1);
            timeholder.yuding = (TextView) convertView.findViewById(R.id.yuding1);
            convertView.setTag(timeholder);
        } else {
            timeholder = (TimeViewHolder) convertView.getTag();
        }

        TimeInfo info = getItem(position);
        timeholder.times.setText(info.getTimes());
        timeholder.price.setText(info.getPrice());
        timeholder.details.setText(info.getDetails());

        timeholder.yuding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(KtvDetailsActivity.this, YudingbaoxiangActivity.class);

                startActivity(intent);


            }
        });

        return convertView;
    }
}

static class TimeViewHolder {
    private TextView times;
    private TextView details;
    private TextView price;
    private TextView yuding;
}

class TeamAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return teamList.size();
    }

    @Override
    public TeamInfo getItem(int position) {
        return teamList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TeamViewHolder teamholder = null;
        if (convertView == null) {
            convertView = UiUtils.inflateView(R.layout.item_teambuy);
            teamholder = new TeamViewHolder();
            teamholder.combo = (TextView) convertView.findViewById(R.id.nametv);
            teamholder.iv = (ImageView) convertView.findViewById(R.id.team_iv);
            teamholder.price = (TextView) convertView.findViewById(R.id.price);
            teamholder.saleprice = (TextView) convertView.findViewById(R.id.pricesale);
            teamholder.yishou = (TextView) convertView.findViewById(R.id.yishou);
            convertView.setTag(teamholder);

        } else {
            teamholder = (TeamViewHolder) convertView.getTag();
        }

        TeamInfo item = getItem(position);
        teamholder.combo.setText(item.getCombo());
        teamholder.iv.setImageDrawable(item.getIv());
        teamholder.price.setText(item.getPrice());
        teamholder.saleprice.setText(item.getSaleprice());
        teamholder.yishou.setText(item.getYishou());

        return convertView;
    }
}

static class TeamViewHolder {
    private ImageView iv;
    private TextView combo;
    private TextView price;
    private TextView saleprice;
    private TextView yishou;
}

class LsAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return lingshiList.size();
    }

    @Override
    public LingshiInfo getItem(int position) {
        return lingshiList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LsViewHolder holder = null;
        if (convertView == null) {
            convertView = UiUtils.inflateView(R.layout.item_lingshi);
            holder = new LsViewHolder();
            holder.iv = (ImageView) convertView.findViewById(R.id.iv);
            holder.price = (TextView) convertView.findViewById(R.id.price);
            holder.tvname = (TextView) convertView.findViewById(R.id.tv);
            holder.add = (TextView) convertView.findViewById(R.id.add);
            convertView.setTag(holder);
        } else {
            holder = (LsViewHolder) convertView.getTag();
        }
        LingshiInfo info = getItem(position);
        holder.iv.setImageDrawable(info.getIv());
        holder.price.setText(info.getPrice());
        holder.tvname.setText(info.getTvname());
        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UiUtils.toast("添加成功");
                //逻辑
                //动画
            }
        });
        return convertView;
    }
}

static class LsViewHolder {
    private ImageView iv;
    private TextView tvname;
    private TextView price;
    private TextView add;
}

private class MyAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return nearktvList.size();
    }

    @Override
    public NearKtvDetilsInfo getItem(int position) {
        return nearktvList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = UiUtils.inflateView(R.layout.item_merchant_ktvdetails);
            holder = new ViewHolder();
            holder.combo = (TextView) convertView.findViewById(R.id.combo);
            holder.name = (TextView) convertView.findViewById(R.id.ktv_name);
            holder.price = (TextView) convertView.findViewById(R.id.price);
            holder.saleprice = (TextView) convertView.findViewById(R.id.fhsmall);
            holder.distance = (TextView) convertView.findViewById(R.id.distance);
            holder.iv = (ImageView) convertView.findViewById(R.id.iv_icon);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        NearKtvDetilsInfo info = getItem(position);
        holder.iv.setImageDrawable(info.getIv());
        holder.combo.setText(info.getCombo());
        holder.distance.setText(info.getDistance());
        holder.name.setText(info.getName());
        holder.price.setText(info.getPrice());
        holder.saleprice.setText(info.getSaleprice());


        return convertView;
    }
}

static class ViewHolder {
    ImageView iv;
    TextView name;
    TextView combo;
    TextView price;
    TextView saleprice;
    TextView distance;
}
}
